#pragma once
#include <iostream>
#include "Comm.hpp"


class Init
{
public:
    Init()
    {
        umask(0);
        int n = ::mkfifo(gpathFile.c_str(),gmode);
        if(n < 0)
        {
            std::cerr << "mkfifo error" << std::endl;
        }
        std::cout << "mkfifo sucess" << std::endl;
    }

    ~Init()
    {
        int n = ::unlink(gpathFile.c_str());
        if(n < 0)
        {
            std::cerr << "unlink error" << std::endl;
        }
        std::cout << "unlink sucess" << std::endl;
    }

};

Init init;


class Server
{
public:
    Server():_fd(gdefultfd)
    {
    }

    ~Server()
    {
    }

    int OpenPipeForRead()
    {
        _fd = OpenPipe(gForRead);
        if(_fd < 0)
        {
            return false;
        }

        return true;
    }

    int RecvPipe(std:: string* out)
    {
        char buffer[gsize];
        ssize_t n = ::read(_fd,buffer,sizeof(buffer)-1);
        if(n > 0)
        {
            buffer[n] = 0;
            *out = buffer;
        }

        return n;
    }

    void ClosePipe()
    {
        CloseHelper(_fd);
    }

private:
    int _fd;
};